package cn.com.bluemoon.daps.model.mapper;

import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.dto.DataTableRelationDto;
import cn.com.bluemoon.daps.model.dto.TableListDto;
import cn.com.bluemoon.daps.model.dto.register.DapModelRegisterDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.vo.IdVo;
import cn.com.bluemoon.daps.model.vo.register.SearchModelRegister;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据模型-模型中设计表的表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Repository
public interface DapDataModelTableMapper extends BaseMapper<DapDataModelTable> {

    /**
     * 查询模型下面表的数量
     */
    int selectTableNum(@Param("modelId") String modelId);

    /**
     * 查询模型下复制的详情列表
     *
     * @param isNow 1表示最新数据，2表示旧数据
     */
    List<DapDataModelTableDto> getCopyTableList(@Param("modelId") String modelId,
                                                @Param("bmStatus") int bmStatus, @Param("isNow") Integer isNow);

    /**
     * 查询模型下复制所有信息
     *
     * @param bmStatus 1表示最新数据，2表示旧数据
     */
    List<DapDataModelTableDto> getCopyTableInfo(@Param("modelId") String modelId,
                                                @Param("bmStatus") int bmStatus);

    /**
     * 查询数据表关联标准结果
     */
    IPage<DataTableRelationDto> selectRelationPage(Page page, @Param("dto") DataTableRelationDto dto);


    /**
     * 更新当前表时间
     */
    @Update("update dap_data_model_table set update_time=current_timestamp(), update_by = #{account} where id = #{tableId}")
    void saveUpdateTime(@Param("tableId") String tableId, @Param("account") String account);

    /**
     * 查询删除未发布的表
     */
    List<DapDataModelTable> selectDeleteTable(@Param("modelId") String modelId);

    /**
     * 删除备份表
     */
    @Delete("delete from dap_data_model_table where data_model_id = #{modelId} and p_id != 0")
    void deleteTable(@Param("modelId") String modelId);

    /**
     * 根据模型id获取对应表下的信息
     *
     * @param dataNewest {@link DapDataModelTable#getDataNewest()}
     */
    List<DapDataModelTableDto> getTablePublishInfos(@Param("modelId") String modelId, @Param("dataNewest") Integer dataNewest);

    /**
     * v1.2版本 根据分组id获得表结构
     */
    List<TableListDto> selectTableByGroupId(@Param("ids") IdVo ids,
                                            @Param("bmStatus") int bmStatus);

    /**
     * 获取模型表信息
     *
     * @param modelId 模型id
     * @param newest  最新
     * @return 获取模型表信息
     */
    List<DapDataModelTable> findByModelIdAndNewest(@Param("modelId") String modelId, @Param("newest") boolean newest);

    /**
     * 获取模型表信息
     *
     * @param modelId 模型id
     * @param newest  最新
     * @param limit   大小
     * @param offset  偏移
     * @return 获取模型表信息
     */
    List<DapDataModelTable> findByModelIdAndNewestLimit(@Param("modelId") String modelId, @Param("newest") boolean newest,
                                                        @Param("offset") int offset, @Param("limit") int limit);

    /**
     * 获取模型注册列表
     */
    IPage<DapModelRegisterDto> selectModelVersionRegister(Page page, @Param("register") SearchModelRegister register);

    /**
     * 获取模型分组下最新表数据（带有是否为共享表标识）
     */
    @Select("select a.*,\n" +
            "IF(a.id = b.table_id, 1, 0) as share_table\n" +
            "from dap_data_model_table a\n" +
            "left join dap_data_model_share_table b\n" +
            "on b.model_id = a.data_model_id and b.table_id = a.id and b.bm_status = 1\n" +
            "where a.bm_status = 1 and a.data_newest = 1 and a.p_id = '0'\n" +
            "and a.data_model_id = #{modelId}\n" +
            "and group_id = #{groupId}")
    List<DapDataModelTable> findNewestTablesByGroupPidAndGroupId(@Param("modelId") String modelId, @Param("groupId") String groupId);

    /**
     * 获取模型表和字段id
     * <pre>
     *     tableId fieldId dataNewest
     *     0      0       1
     *     xxx    xxxx    0
     * </pre>
     */
    @Select("select a.p_id as tableId,\n" +
            "       b.p_id as fieldId,\n" +
            "       b.data_newest as dataNewest\n" +
            "from dap_data_model_table a\n" +
            "         left join dap_data_model_table_field b on b.model_table_id = a.id and b.bm_status = 1\n" +
            "where a.data_model_id = #{modelId}\n" +
            "  and a.eng_name = #{tableName}\n" +
            "  and a.bm_status = 1\n" +
            "  and b.field_name = #{fieldName}\n" +
            "  limit 2")
   List<Map<String, Object>> findTableAndFieldId(@Param("modelId") String modelId, @Param("tableName") String tableName, @Param("fieldName") String fieldName);
}
